<?php
class Warehouse_Service_Products
{
	public function __construct(){
		
	}

	public static function add($row){
		$Obj = new Warehouse_Model_Products();
		return $Obj->add($row);		
	}
	
	public static function update($row,$id){
		$Obj = new Warehouse_Model_Products();
		return $Obj->update($row,$id);		
	}
	
	public static function delete($id){
		$Obj = new Warehouse_Model_Products();
		return $Obj->delete($id);		
	}
		
	public static function getByProductId($ProductId){
		$obj = new Warehouse_Model_Products();
		$Rows=$obj->getByProductId($ProductId);
		if(!empty($Rows)){
			$Rows=$Rows->toArray();
		}
		return $Rows;
	}
	
	public static function getByAllProductId($ProductId,$warehouseId=1){
		$obj = new Warehouse_Model_Products();
		$Rows=$obj->getByAllProductId($ProductId,$warehouseId);
		return $Rows;
	}
	
	public static function getByJoinWSCondition($condition=array(),$type='*',$order=array(), $pageSize=0, $page=0)
	{ 
		$obj=new Warehouse_Model_Products();
		$rows= $obj->getByJoinWSCondition( $condition,$type,$order, $pageSize,$page);
		return $rows;
	}
	
	
	public static function getByJoinInWsCodeCon($condition=array(),$type='*',$order=array(), $pageSize=0, $page=0)
	{
		$obj=new Warehouse_Model_Products();
		$rows= $obj->getByJoinInWsCodeCon( $condition,$type,$order, $pageSize,$page);
		return $rows;
	}
	
	/*
	*判断区域对应目录内是否存在不相同目录的产品
	*/
	public static function getByAreaValidateProductCondition($condition=array(),$type='*',$order=array(), $pageSize=0, $page=0)
	{ 
		if(!isset($condition["select_poc_id"]) && $condition["select_poc_id"]==''){
			return array('ask'=>0,'message'=>'请选择产品目录!');
		}
		$result=array('ask'=>1,'message'=>'');
		$obj=new Warehouse_Model_Products();
		$productmodel= new Product_Model_ProductOptionValue();
		$rows= $obj->getByJoinWSCondition( $condition,$type,$order, $pageSize,$page);
		$wscode="";
		foreach($rows as $key => $val){
			$RowAll=$productmodel->getByProductId($val['product_id']);
			if(!empty($RowAll)){
				if($RowAll[0]['poc_id']!=$condition['select_poc_id']){//修改的目录ID
					$wscode.=$val['ws_code']." ";
				}
			}
		}
		if(!empty($wscode)){
			$result=array('ask'=>0,'message'=>'货架:'.$wscode);
		}
		return $result;
	}
	
	public static function getByCond($condition=array(),$type='*',$order=array(), $pageSize=0, $page=0)
	{ 
		$obj=new Warehouse_Model_Products();
		$rows= $obj->getByCond( $condition,$type,$order, $pageSize,$page);
		return $rows;
	}
	
	public static function getByCondition($condition=array()){
		$obj = new Warehouse_Model_Products();
		$Rows=$obj->getByCondition($condition);
		return $Rows;
	}
	
	public static function getByConditionAll($condition=array()){
		$obj = new Warehouse_Model_Products();
		$Rows=$obj->getByConditionAll($condition);
		return $Rows;
	}
	 
	public static function getByWpid($pp_id){		
		$obj = new Warehouse_Model_Products();
		return $obj->getByWpid($pp_id);		
	}
	
	public function getJoinByCond($cond,$type,$order, $pageSize, $page){
		$Obj = new Warehouse_Model_Products();	
		return $Obj->getJoinByCond($cond,$type,$order, $pageSize, $page);		
	}
	
	 public static  function updateQuantityHold($row,$awhere){
	 	$Obj = new Warehouse_Model_Products();
	 	return $Obj->updateQuantityHold($row,$awhere);
	 }
	 	
	//货架日志
	public static function CreateWarehouseProductsLog($row)
	{
        $session = new Zend_Session_Namespace('userAuth');
        $row['user_id']=isset($session->userID)?$session->userID:0;
		$sIP = $_SERVER['REMOTE_ADDR'];
		if($sIP==''&$sIP==null) $sIP = "127.0.0.1";
		 
        $row['wpl_ip']= $sIP;
        $row['wpl_add_time']=date('Y-m-d H:i:s');
		$Obj = new Warehouse_Model_ProductsLog();
		return $Obj->create($row);		
	}

	/**
	 * 根据字段值 获取指定内容
	 * @param string $field
	 * @param string $value
	 * @param int $type 1:返回一维数组；2：返回二维数组
	 * @return 数组
	 */
	 public static function getByfield($field,$value,$type=2)
	{ 
		$Obj = new Warehouse_Model_Products();
		$rows=$Obj->getByfield($field,$value,$type);
		if(!empty($rows)){
			$rows=$rows->toArray();
		}
		return $rows;
	}
		
	/**
	 * 针对库存盘点读取数据
	 * $cond查询条件， 索引为数据表的字段
	 * @return 二维数组
	 */
	public static function getForStocktake($cond,$type,$pageSize=0,$page=0,$orderby=array()){ 
		$obj=new Warehouse_Model_Products();
		$rows= $obj->getForStocktake($cond,$type,$pageSize,$page,$orderby);	
		return ($rows);
	}

}